{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get 查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@courses_bp.route(\"/models/all\", methods=[\"GET\"])\n",
    "def get_all_models():\n",
    "    \"\"\"\n",
    "    查询所有模型信息\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    sql = \"select * from models\"\n",
    "    try:\n",
    "        cursor.execute(sql)\n",
    "        user_data = cursor.fetchall()\n",
    "    except:\n",
    "        import traceback\n",
    "\n",
    "        traceback.print_exc()\n",
    "    if user_data:\n",
    "        return jsonify({\"code\": 200, \"data\": user_data, \"msg\": \"查询成功\"})\n",
    "    return jsonify({\"code\": 201, \"msg\": \"查询失败\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@courses_bp.route(\"/models/orientation\", methods=[\"GET\"])\n",
    "def get_part_models():\n",
    "    \"\"\"\n",
    "    查询某方向所有模型信息\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    orientation = request.args.get(\"orientation\")\n",
    "    sql = \"select * from models where ModelCate = '%s'\" % orientation\n",
    "    try:\n",
    "        cursor.execute(sql)\n",
    "        user_data = cursor.fetchall()\n",
    "\n",
    "    except:\n",
    "        import traceback\n",
    "\n",
    "        traceback.print_exc()\n",
    "    if user_data:\n",
    "        return jsonify({\"code\": 200, \"data\": user_data, \"msg\": \"查询成功\"})\n",
    "    return jsonify({\"code\": 201, \"msg\": \"查询失败\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@courses_bp.route(\"/models/single\", methods=[\"GET\"])\n",
    "def get_single_models():\n",
    "    \"\"\"\n",
    "    查询某个模型信息\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    modelname = request.args.get(\"modelname\")\n",
    "    sql = \"select * from models where Modelname = '%s'\" % modelname\n",
    "    try:\n",
    "        cursor.execute(sql)\n",
    "        user_data = cursor.fetchone()\n",
    "\n",
    "    except:\n",
    "        import traceback\n",
    "\n",
    "        traceback.print_exc()\n",
    "    if user_data:\n",
    "        return jsonify({\"code\": 200, \"data\": user_data, \"msg\": \"查询成功\"})\n",
    "    return jsonify({\"code\": 201, \"msg\": \"查询失败\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@courses_bp.route(\"/invokings\", methods=[\"POST\"])\n",
    "def get_invoking_info():\n",
    "    \"\"\"\n",
    "    查询调用记录\n",
    "    :return:\n",
    "    \"\"\"\n",
    "\n",
    "    try:\n",
    "        modelname = request.args.get(\"modelname\")\n",
    "        sql1 = \"select ModelCate from models where Modelname = '%s'\" % modelname\n",
    "        cursor.execute(sql1)\n",
    "        modelcate = cursor.fetchone().field(0)\n",
    "        sql2 = \"select * from '%s' where \"\n",
    "    except:\n",
    "        import traceback\n",
    "\n",
    "        traceback.print_exc()\n",
    "    if user_data:\n",
    "        return jsonify({\"code\": 200, \"data\": user_data, \"msg\": \"查询成功\"})\n",
    "    return jsonify({\"code\": 201, \"msg\": \"查询失败\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@courses_bp.route(\"/isexists\", methods=[\"post\"])\n",
    "def is_exists():\n",
    "    \"\"\"\n",
    "    检查某项信息是否存在数据库中\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    # eg: {\"request\": \"Modelname\", \"info\": \"语音合成1\"}\n",
    "    field = request.json.get(\"request\").strip()\n",
    "    info = request.json.get(\"info\").strip()\n",
    "    if field == \"Username\" or field == \"Telephone\":\n",
    "        table = \"users\"\n",
    "    elif field == \"Modelname\":\n",
    "        table = \"models\"\n",
    "    else:\n",
    "        return jsonify({\"code\": 500, \"msg\": \"请求的字段不存在!\"})\n",
    "    sql = \"select count(*) from %s where %s = '%s'\" % (table, field, info)\n",
    "    try:\n",
    "        cursor.execute(sql)\n",
    "        count = cursor.fetchone()[0]\n",
    "    except:\n",
    "        import traceback\n",
    "\n",
    "        traceback.print_exc()\n",
    "\n",
    "    if count == 0:\n",
    "        return jsonify({\"code\": 200})\n",
    "    return jsonify({\"code\": 500, \"msg\": \"%s已存在\" % field})"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
